const { Service } = require('egg');
const { v4: uuidV4 } = require('uuid');
const moment = require('moment');
const product_table = 'product',
  user_table = 'user',
  barcode_table = 'barcode';

class BarcodeService extends Service {
  async list(product_id, name, barcode, method, order, pageIndex, pageSize) {
    const ctx = this.ctx;
    try {
      const sql = ctx.app.knex().select('t2.name as product_name', 't1.uuid', 't1.content as barcode',
        't1.create_time', 't1.update_time', 't3.name as create_name', 't4.name as update_name'
      )
        .from(`${barcode_table} as t1`)
        .leftJoin(`${product_table} as t2`, function() {
          this.on('t1.product_id', '=', 't2.uuid');
        })
        .leftJoin(`${user_table} as t3`, function() {
          this.on('t1.create_by', '=', 't3.uuid');
        })
        .leftJoin(`${user_table} as t4`, function() {
          this.on('t1.update_by', '=', 't4.uuid');
        })
        .where('t1.is_delete', 0)
        .andWhere('t2.is_delete', 0);

      const countSql = ctx.app.knex().count('t1.uuid as count')
        .from(`${barcode_table} as t1`)
        .leftJoin(`${product_table} as t2`, function() {
          this.on('t1.product_id', '=', 't2.uuid');
        })
        .where('t1.is_delete', 0)
        .andWhere('t2.is_delete', 0);

      if (product_id) {
        sql.andWhere('t2.uuid', '=', `${product_id}`);
        countSql.andWhere('t2.uuid', '=', `${product_id}`);
      }

      if (name) {
        sql.andWhereLike('t2.name', `%${name}%`);
        countSql.andWhereLike('t2.name', `%${name}%`);
      }

      if (barcode) {
        sql.andWhereLike('t1.content', `${barcode}`);
        countSql.andWhereLike('t1.content', `${barcode}`);
      }

      sql.orderBy(order, method).limit(Number(pageSize)).offset((Number(pageIndex) - 1) * Number(pageSize));

      const list = await sql;
      const count = await countSql.first();
      const result = {
        list,
        count: count.count,
      };


      return result;
    } catch (error) {
      throw error;
    }
  }

  // 新增条码
  async insert(product_id, barcode) {
    const ctx = this.ctx;
    try {
      const time = moment();
      const { id: user_id } = ctx.session;
      const instance = {
        uuid: uuidV4(),
        content: barcode,
        product_id,
        create_time: moment(time).format('YYYY-MM-DD HH:mm:ss'),
        create_by: user_id,
        update_by: user_id,
        update_time: moment(time).format('YYYY-MM-DD HH:mm:ss'),
      };

      await ctx.app.knex(barcode_table).insert(instance);

      return instance;
    } catch (error) {
      throw error;
    }
  }

  // 删除条码
  async delete(ids) {
    const ctx = this.ctx;
    try {
      const time = moment();
      const { id: user_id } = ctx.session;

      const instance = {
        update_by: user_id,
        update_time: moment(time).format('YYYY-MM-DD HH:mm:ss'),
        is_delete: 1,
      };

      await ctx.app.knex(barcode_table).update(instance).whereIn('uuid', ids);


      return;
    } catch (error) {
      throw error;
    }
  }
}

module.exports = BarcodeService;
